/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ps.server;

import com.ps.server.ManufacturerLibraryXMLReader.langTemp;
import com.ps.server.classes.languages;
import com.ps.server.classes.manufacturers;
import com.ps.server.classes.suppliers;
import com.ps.server.classes.zones;
import java.util.ArrayList;
import java.util.Vector;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author Ricardo
 */
public class SupplierLibraryXMLReader extends DefaultHandler {

    Vector nodos = new Vector();
    String contenido = "";
    String language = "";
    private ArrayList<langTemp> descriptionLanguage;
    private suppliers nodoActualSuppliers = new suppliers();
    String resource;
    private langTemp tem;
    //private clsProduct nodoActual = new clsProduct();

    public SupplierLibraryXMLReader(Vector nodos) {
        this.nodos = nodos;
    }

    public SupplierLibraryXMLReader() {
    }
    /*
     * Esta funcion el llamada cuando se produce el evento de ver una nueva
     * etiqueta
     */

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) {

        if ("supplier".equals(qName)) {
            this.resource = qName;
            nodoActualSuppliers = new suppliers();
        }
        if ("meta_title".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("meta_description".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("meta_keywords".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("description".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("language".equals(qName)) {
            this.tem.setId(attributes.getValue("id"));
            //this.short_description
            // this.idlang=attributes.getValue("id");
            //this.meta_keywords = new ArrayList();
        }


    }
    /*
     * Esta funcion es llamada cuando ve el contenido de una etiqueta
     */

    @Override
    public void characters(char buf[], int offset, int len)
            throws SAXException {
        contenido = new String(buf, offset, len);

    }
    /*
     * y esta al llegar al final
     */

    @Override
    public void endElement(String uri, String localName, String qName) {

        if (this.resource.equals("supplier")) {
            resourceSuppliers(qName);
        }


    }

    void resourceSuppliers(String qName) {
        if ("id".equals(qName)) {
                nodoActualSuppliers.setId(contenido);
            }
        
        if ("link_rewrite".equals(qName)) {
            nodoActualSuppliers.setLink_rewrite(contenido);
        }
        if ("name".equals(qName)) {
            nodoActualSuppliers.setName(contenido);
        }

        if ("meta_title".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualSuppliers.setMeta_title(shortString);
        }

        if ("language".equals(qName)) {
            langTemp t = new langTemp();
            t.setId(this.tem.getId());
            t.setValue(contenido);
            this.descriptionLanguage.add(t);

            //nodoActualManufacturers.setId(contenido);
        }

        if ("meta_description".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualSuppliers.setMeta_description(shortString);
        }
        if ("meta_keywords".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualSuppliers.setMeta_keywords(shortString);
        }

        if ("description".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualSuppliers.setDescription(shortString);
        }
        
        if ("supplier".equals(qName)) {
            this.nodos.add(nodoActualSuppliers);
            this.contenido = this.nodoActualSuppliers.toXML();
            //nodoActualManufacturers.setId(contenido);
        }

    }

    class langTemp {

        private String id;
        private String value;

        public void langTemp() {
        }

        public void langTemp(String id, String value) {
            this.setId(id);
            this.setValue(value);
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getValue() {
            if (value == null) {
                value = "";
            }
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }
}